"""Write Eset

.. helpdoc::
This widget writes an eset signal to a file.  These objects could then be shared as separate objects with other users.  In most cases it might be better to actually export individual objects as tables which would be more generic.  Because of this, this widget will undergo little development until prompting from a user.

"""

"""<widgetXML>
<name>Write eSet</name>
    <icon>Default.png</icon>
    <tags> 
        <tag>Microarray</tag> 
    </tags>
    <summary>Writes an eSet to a tab delimited file.</summary>
    <details>Writes an eSet to a tab delimited file.</details>
    <author>
        <authorname>Kyle R Covington</authorname>
        <authorcontact>kyle@red-r.org</authorcontact>
    </author>
</widgetXML>
"""

"""
<name>Write eSet</name>
<description>Writes an eSet to a tab delimited file.</description>
<tags>Affy Files</tags>
"""
from OWRpy import *
import redRGUI
import libraries.base.signalClasses.RVariable as rvar
## depricated may bring back later
class writeEset(OWRpy):
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        
        self.data = None
        self.fileName = ""
        self.saveSettingsList.extend(['data', 'fileName'])
        
        """.. rrsignals::"""
        self.inputs.addInput('ab1', "Affybatch", rvar.RVariable, self.nothingb)
        
        #GUI
        
        """.. rrgui::"""
        box = redRWidgetBox(self.controlArea, "Write to file.")
        
        #redRGUI.lineEdit(box, label = "File Name", orientation = "horizontal") 
        writeButton = redRCommitButton(box, "Write to file", callback = self.write, width=200)
        
    def nothingb(self,data):
        if data:
            self.data = data.getData()
        else: return
            
    def write(self):
        if not self.require_librarys(['affy']):
            self.status.setText('R Libraries Not Loaded.')
            return
        if self.data == None or self.data == '':
            self.status.setText("Data has not been loaded yet")
            return
        if ('HomeFolder' not in qApp.canvasDlg.settings.keys()):
            file = unicode(QFileDialog.getSaveFileName(self, "Save File", os.path.abspath(qApp.canvasDlg.settings['saveSchemaDir']), "Plain Text (*.txt)"))
        else: 
            file = unicode(QFileDialog.getSaveFileName(self, "Save File", os.path.abspath(qApp.canvasDlg.settings['HomeFolder']), "Plain Text (*.txt)"))

        self.R('write.exprs('+self.data+',file="'+file+'", sep="\t")', wantType = 'NoConversion')
        self.status.setText("Data was writen to "+file+" successfully!")
        self.notes.setCursorToEnd()
        self.notes.insertHtml('<br>File Saved as: '+file+'<br>')
        